using System;
using UnityEngine;

namespace GamePlay.Features.Shared
{
    /// <summary>
    /// 玩家数据类，保存玩家信息
    /// </summary>
    [Serializable]
    public class PlayerData
    {
        #region 基本属性

        // 玩家ID
        public string PlayerId;

        // 玩家名称
        public string PlayerName;

        // 玩家等级
        public int Level;

        // 经验值
        public int Exp;

        // 金币
        public int Gold;

        // 钻石
        public int Diamond;

        // 玩家当前健康值
        public int CurrentHealth;

        // 玩家最大健康值
        public int MaxHealth;

        // 创建时间
        public DateTime CreateTime;

        // 最后登录时间
        public DateTime LastLoginTime;

        #endregion

        #region 游戏进度

        // 当前解锁的关卡
        public int UnlockedLevel;

        // 最高通关关卡
        public int HighestCompletedLevel;

        // 玩家总游戏时间（秒）
        public float TotalPlayTime;

        #endregion

        #region 统计数据

        // 总得分
        public int TotalScore;

        // 最高得分
        public int HighestScore;

        // 游戏总局数
        public int TotalGames;

        // 胜利局数
        public int WinGames;

        #endregion

        /// <summary>
        /// 创建新玩家数据
        /// </summary>
        /// <param name="playerId">玩家ID</param>
        /// <param name="playerName">玩家名称</param>
        /// <returns>新的玩家数据</returns>
        public static PlayerData CreateNew(string playerId, string playerName)
        {
            return new PlayerData
            {
                PlayerId = playerId,
                PlayerName = playerName,
                Level = 1,
                Exp = 0,
                Gold = 100,
                Diamond = 10,
                CurrentHealth = 100,
                MaxHealth = 100,
                CreateTime = DateTime.Now,
                LastLoginTime = DateTime.Now,
                UnlockedLevel = 1,
                HighestCompletedLevel = 0,
                TotalPlayTime = 0,
                TotalScore = 0,
                HighestScore = 0,
                TotalGames = 0,
                WinGames = 0
            };
        }

        /// <summary>
        /// 添加经验值并处理升级
        /// </summary>
        /// <param name="amount">经验值数量</param>
        /// <returns>是否升级</returns>
        public bool AddExp(int amount)
        {
            Exp += amount;

            // 检查是否升级 (简单公式: 当前等级*100经验升级)
            int expRequired = Level * 100;

            if (Exp >= expRequired)
            {
                Exp -= expRequired;
                Level++;

                // 升级时增加最大健康值
                MaxHealth = 100 + (Level - 1) * 20;
                CurrentHealth = MaxHealth;

                return true;
            }

            return false;
        }

        /// <summary>
        /// 添加金币
        /// </summary>
        /// <param name="amount">金币数量</param>
        public void AddGold(int amount)
        {
            Gold += amount;
        }

        /// <summary>
        /// 消费金币
        /// </summary>
        /// <param name="amount">金币数量</param>
        /// <returns>是否消费成功</returns>
        public bool SpendGold(int amount)
        {
            if (Gold >= amount)
            {
                Gold -= amount;
                return true;
            }
            return false;
        }

        /// <summary>
        /// 添加钻石
        /// </summary>
        /// <param name="amount">钻石数量</param>
        public void AddDiamond(int amount)
        {
            Diamond += amount;
        }

        /// <summary>
        /// 消费钻石
        /// </summary>
        /// <param name="amount">钻石数量</param>
        /// <returns>是否消费成功</returns>
        public bool SpendDiamond(int amount)
        {
            if (Diamond >= amount)
            {
                Diamond -= amount;
                return true;
            }
            return false;
        }

        /// <summary>
        /// 更新玩家最后登录时间
        /// </summary>
        public void UpdateLastLoginTime()
        {
            LastLoginTime = DateTime.Now;
        }

        /// <summary>
        /// 添加游戏时间
        /// </summary>
        /// <param name="seconds">游戏时间（秒）</param>
        public void AddPlayTime(float seconds)
        {
            TotalPlayTime += seconds;
        }

        /// <summary>
        /// 记录游戏结果
        /// </summary>
        /// <param name="isWin">是否胜利</param>
        /// <param name="score">得分</param>
        public void RecordGameResult(bool isWin, int score)
        {
            TotalGames++;

            if (isWin)
            {
                WinGames++;
            }

            TotalScore += score;

            if (score > HighestScore)
            {
                HighestScore = score;
            }
        }

        /// <summary>
        /// 解锁新关卡
        /// </summary>
        /// <param name="level">关卡编号</param>
        /// <returns>是否成功解锁</returns>
        public bool UnlockLevel(int level)
        {
            if (level > UnlockedLevel && level <= HighestCompletedLevel + 1)
            {
                UnlockedLevel = level;
                return true;
            }
            return false;
        }

        /// <summary>
        /// 完成关卡
        /// </summary>
        /// <param name="level">关卡编号</param>
        public void CompleteLevel(int level)
        {
            if (level > HighestCompletedLevel)
            {
                HighestCompletedLevel = level;

                // 自动解锁下一关
                if (level + 1 > UnlockedLevel)
                {
                    UnlockedLevel = level + 1;
                }
            }
        }

        /// <summary>
        /// 恢复生命值
        /// </summary>
        /// <param name="amount">恢复量</param>
        public void Heal(int amount)
        {
            CurrentHealth = Mathf.Min(CurrentHealth + amount, MaxHealth);
        }

        /// <summary>
        /// 受到伤害
        /// </summary>
        /// <param name="amount">伤害量</param>
        /// <returns>是否死亡</returns>
        public bool TakeDamage(int amount)
        {
            CurrentHealth = Mathf.Max(0, CurrentHealth - amount);
            return CurrentHealth <= 0;
        }
    }
}